import QtQuick 1.0
import QtMobility.gallery 1.1
import QtMobility.sensors 1.1
import QtMultimediaKit 1.1
import com.nokia.symbian 1.0

// The skeleton of this page is provided by Daniel.Wilms@nokia.com
// Thanks to him

Page {
    property int i: 0; // index of the audio file in the gallery

    id: page2
    anchors.fill: parent
    orientationLock: PageOrientation.LockPortrait
    tools: toolBarMainPage

    Rectangle{
        anchors.fill: parent
        gradient: Gradient {
            GradientStop {
                position: 0
                color: platformStyle.colorNormalDark
            }
            GradientStop {
                position: 1
                color: platformStyle.colorNormalLight
            }
        }
        DocumentGalleryModel {
            id: galleryModel
            rootType : DocumentGallery.Audio
            properties : [ "url", "artist", "title"]

        }
        Audio {
            id: playMusic
            onStarted:{
                artistLabel.text = "Artist - " + galleryModel.get(i).artist
                titleLabel.text = "Title - " + galleryModel.get(i).title
            }
        }
        Column{
            anchors.centerIn: parent
            spacing: platformStyle.paddingLarge
            Text {
                id: artistLabel
                anchors.horizontalCenter: parent.horizontalCenter
                color: platformStyle.colorNormalLight
                wrapMode: Text.WordWrap
                width: parent.width
                horizontalAlignment: Text.AlignHCenter
                font.pixelSize: platformStyle.fontSizeHuge
                font.weight: Font.Bold
                text: "Artist - NA"
            }
            Text {
                id: titleLabel
                anchors.horizontalCenter: parent.horizontalCenter
                font.pixelSize: platformStyle.fontSizeMedium
                color: platformStyle.colorNormalLight
                wrapMode: Text.WordWrap
                width: parent.wdith
                horizontalAlignment: Text.AlignHCenter
                font.weight: Font.Bold
                text: "Title - NA"
            }
            Button {
                id: playbutton
                width: parent.width
                anchors.horizontalCenter: parent.horizontalCenter
                text: "Play"
                onClicked:{
                    playMusic.source=galleryModel.get(i).url
                    if(!playMusic.playing){
                        playMusic.play();
                        text="Stop"
                    }
                    else{
                        playMusic.stop();
                        text="Play"
                    }
                }
            }
        }
        Accelerometer{
            active: playMusic.playing
            onReadingChanged:{
                if((reading.x>10 || reading.x<-10) && playMusic.status!= Audio.Loading){
                    i= Math.floor( Math.random ( ) * galleryModel.count )
                    playMusic.source=galleryModel.get(i).url
                    playMusic.stop()
                    playMusic.play();
                }
            }
        }
    }
}
